package com.groupbuying.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.groupbuying.admin.connector.StoreFeign;
import com.groupbuying.admin.constants.SysAgentInfoEnumConstants;
import com.groupbuying.admin.context.FilterContextHandler;
import com.groupbuying.admin.context.FilterContextHandler;
import com.groupbuying.admin.domain.AgentAuditStoreDO;
import com.groupbuying.admin.dto.AgentDTO;
import com.groupbuying.admin.dto.AgentStoreAccountDTO;
import com.groupbuying.admin.dto.SysAgentFlowDTO;
import com.groupbuying.admin.dto.do2dto.AgentConvert;
import com.groupbuying.admin.service.AgentAuditStoreService;
import com.groupbuying.admin.utils.*;
import com.groupbuying.admin.utils.apiresult.ApiResult;
import com.groupbuying.admin.utils.apiresult.ApiResultCode;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.groupbuying.admin.domain.SysAgentInfoDO;
import com.groupbuying.admin.service.SysAgentInfoService;

/**
 * 代理商信息表
 * 
 * @author WANGNIGN
 * @email wyn20140226@aliyun.com
 * @date 2019-07-22 17:48:37
 */
 
@RestController
@RequestMapping("/agent")
public class SysAgentInfoController {
	@Autowired
    private SysAgentInfoService sysAgentInfoService;

    @Autowired
    private AgentAuditStoreService agentAuditStoreService;

	@Autowired
	private StoreFeign storeFeign;
	private static final Logger logger = LoggerFactory.getLogger(SysAgentInfoController.class);
	@GetMapping()
	@RequiresPermissions("admin:sysAgentInfo:sysAgentInfo")
	String SysAgentInfo(){
	    return "admin/sysAgentInfo/sysAgentInfo";
	}
	
	@GetMapping("/list")
	@RequiresPermissions("admin:sysAgentInfo:sysAgentInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        String agentId = FilterContextHandler.getAgentId();
		if(StringUtils.isEmpty(agentId)){
			agentId = "00000";
		}
		params.put("agentId",agentId);
		Query query = new Query(params);
		List<SysAgentInfoDO> sysAgentInfoList = sysAgentInfoService.list(query);
		int total = sysAgentInfoService.count(query);
		PageUtils pageUtils = new PageUtils(sysAgentInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("admin:sysAgentInfo:add")
	String add(){
	    return "admin/sysAgentInfo/add";
	}

	@PutMapping("/edit")
	public ApiResult edit(@RequestBody SysAgentInfoDO agentInfoDO){
		logger.error("SysAgentInfoController-->edit params:::"+ JsonUtils.objectToJson(agentInfoDO));
		return  sysAgentInfoService.edit(agentInfoDO);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("admin:sysAgentInfo:add")
	public R save( SysAgentInfoDO sysAgentInfo){
		if(sysAgentInfoService.save(sysAgentInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("admin:sysAgentInfo:edit")
	public R update( SysAgentInfoDO sysAgentInfo){
		sysAgentInfoService.update(sysAgentInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@RequiresPermissions("admin:sysAgentInfo:remove")
	public R remove( String id){
		if(sysAgentInfoService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@RequiresPermissions("admin:sysAgentInfo:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		sysAgentInfoService.batchRemove(ids);
		return R.ok();
	}
	/**
	 * 代理商注册
	 * @author cuich
	 * @date 2019年7月26日14:08:56
	 */
	@PostMapping("/regist")
	public ApiResult regist(@RequestBody SysAgentInfoDO agentInfoDO){
		logger.error("SysAgentInfoController-->regist params:::"+ JsonUtils.objectToJson(agentInfoDO));
		return sysAgentInfoService.regist(agentInfoDO);
	}
	/**
	 * 通过代理商id添加服务商
	 */
	@PostMapping("/addStoreAccount")
	public ApiResult saveStoreAccount(@RequestBody AgentStoreAccountDTO agentStoreAccountDTO) {
		logger.error("SysAgentInfoController-->saveStoreAccount"+ agentStoreAccountDTO.toString());
		Map<String,Object> map =new HashedMap<>();
		map.put("accountName", agentStoreAccountDTO.getAccountName());
		map.put("mobile", agentStoreAccountDTO.getMobile());
		map.put("name", agentStoreAccountDTO.getName());
		map.put("password", agentStoreAccountDTO.getPassword());
		map.put("providerName", agentStoreAccountDTO.getProviderName());
		logger.error("saveStoreAccount.saveStoreAccount() params:::"+JsonUtils.objectToJson(map));
		ApiResult result=sysAgentInfoService.addAccount(map);
		logger.error("saveStoreAccount.saveStoreAccount() result:::"+JsonUtils.objectToJson(result));
		return result;
	}
	/**
	 * 修改信息
	 */
	@RequestMapping("/updateStatus")
	public R updateStatus( @RequestParam Map<String, Object> params){
		int updateStatus=this.sysAgentInfoService.updateStatus(params);
		if(updateStatus>0){
			logger.info("SysAgentInfoController.updateStatus()----->更改数据成功！");
			return R.ok();
		}else{
			return R.error();
		}

	}
	/**
	 * 获取代理商流水信息
	 */
	@ResponseBody
	@GetMapping("/agentFlowList")
	public PageUtils agentFlowList(@RequestParam Map<String, Object> params){
		/*从token获取代理商id*/
		String agentId = FilterContextHandler.getAgentId();
		logger.info("SysAgentInfoController.agentFlowList()----->params:"+params.toString());
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商id为:"+agentId);
		params.put("agentId", agentId);
		//查询列表数据
		//--Query query = new Query(params);
        PageUtils sysAgentInfoList = sysAgentInfoService.agentFlowList(params);
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商sysAgentInfoList.total:"+sysAgentInfoList.getTotal());
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商sysAgentInfoList.rows:"+sysAgentInfoList.getRows());
		return sysAgentInfoList;
	}

	/**
	 * 通过代理商id获取服务商列表信息
	 */
	@GetMapping("/getStoreAccountList")
	public PageUtils getStoreAccountList(@RequestParam Map<String, Object> params){

		return sysAgentInfoService.getStoreAccountList(params);
	}
	/**
	 * 通过代理商id获取服务商门店列表信息
	 */
	@GetMapping("/storeMainInfoListByAgent")
	public PageUtils storeMainInfoListByAgent(@RequestParam Map<String, Object> params){

		return sysAgentInfoService.storeMainInfoListByAgent(params);
	}
	/**
	 * 通过代理商id获取门店运营人员列表信息
	 */
	@GetMapping("/getStoreOperList")
	public PageUtils getStoreOperList(@RequestParam Map<String, Object> params){
		logger.info("SysAgentInfoController.getStoreOperList()----->获取到的代理商门店运营人员为:"+params.toString());
		return sysAgentInfoService.getStoreOperList(params);
	}

    /**
     * 查询代理商注册的服务商列表
     * @auth cuich
     * @date 2019年8月5日15:28:24
     */
    @GetMapping("/getStoreList")
    public PageUtils getStoreList(@RequestParam Map<String, Object> params){
        logger.info("SysAgentInfoController-->getStoreList() params:::"+JsonUtils.objectToJson(params));
        Query query = new Query(params);
        List<AgentAuditStoreDO> agentAuditStoreList = agentAuditStoreService.list(query);
        int total = agentAuditStoreService.count(query);
        PageUtils page = new PageUtils(agentAuditStoreList,total);
        return page;
    }
	/**
	 * 更改store中心审核状态
	 */
    @RequestMapping("/updateStoreStatus")
    public ApiResult updateStoreStatus( @RequestParam Map<String, Object> params){
        ApiResult updateStatus=this.sysAgentInfoService.updateStoreStatus(params);
        if(updateStatus.get("code").equals(ApiResultCode.SUCCESS.code())){
            logger.info("SysAgentInfoController-->updateStoreStatus 修改成功！");
            return ApiResult.success();
        }else{
            logger.info("SysAgentInfoController-->updateStoreStatus 修改失败！");
            return ApiResult.failure();
        }

    }
    /**
     * 通过代理商id查询代理商信息
     */
	@RequestMapping("/queryAgent")
	public String queryAgent( @RequestParam Map<String, Object> params){
		logger.info("SysAgentInfoController.queryAgent() ----->通过代理商id查询代理商信息"+params.toString());
		String id = (String)params.get("agentId");
		SysAgentInfoDO agentInfoDO=this.sysAgentInfoService.get(id);
		String agentInfoJson=JsonUtils.beanToJson(agentInfoDO,"yyyyMMdd");
		return agentInfoJson;

	}

	@GetMapping("/queryAgentByArea")
	public Object queryAgentByArea( @RequestParam Map<String, Object> params){
		logger.error("SysAgentInfoController.queryAgentByArea() ----->通过代理商id查询代理商信息{}",JsonUtils.objectToJson(params));
		params.put("status", SysAgentInfoEnumConstants.ENUM_STATUS_START);
		List<SysAgentInfoDO>  sysAgentInfoDOS=this.sysAgentInfoService.list(params);
		String agentInfoJson=JsonUtils.beanToJson(sysAgentInfoDOS,"yyyyMMdd");
		return agentInfoJson;
	}
	/**
	 * 获取状态正常的代理商列表
	 * @auth wgq
	 * @date 2019年9月25日15:28:24
	 */
	@GetMapping("/getStartStatusAgentList")
	public R getStartStatusAgentList(){
		Map map = new HashMap();
		map.put("status", SysAgentInfoEnumConstants.ENUM_STATUS_START);
		List<SysAgentInfoDO> sysAgentInfoList = sysAgentInfoService.list(map);
		List<AgentDTO> agentDTOList = new ArrayList<>(sysAgentInfoList.size());
		for (SysAgentInfoDO SysAgentInfoDO : sysAgentInfoList){
			agentDTOList.add(AgentConvert.MAPPER.do2dto(SysAgentInfoDO));
		}
		return R.data(agentDTOList);
	}
}
